//
//  WordOfMouthController.m
//  YongChuanTradingArea
//
//  Created by 李冰 on 2017/9/20.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "WordOfMouthController.h"
#import "WordOfMouthCell.h"
#import "WordOfMouthModel.h"

#import "UserCommentListController.h" // 评论详情

static NSString * const WordOfMouthCellIde = @"WordOfMouthCellIde";
@interface WordOfMouthController ()

@end

@implementation WordOfMouthController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"口碑管理";
    
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = kBgColor;
    self.tableView.rowHeight = 80;
    [self setupPageHeader:YES footer:YES];
    [self headerRefresh];
    [self.tableView registerNib:[UINib nibWithNibName:@"WordOfMouthCell" bundle:nil] forCellReuseIdentifier:WordOfMouthCellIde];
    
}

- (void)requestPageData:(void (^)(YCNetworkingStatusCode, NSArray *))statusBlock {

    if (![LBUserManage defaultManager].memberId)
    {
        [MBProgressHUD showMessageWithOutIcon:@"请登录后尝试"];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        return;
    }
    
    NSString *userId = [LBUserManage defaultManager].memberId;
    NSString *pageIndex = [NSString stringWithFormat:@"%ld", self.pageIndex];
    NSString *pageSize = [NSString stringWithFormat:@"%ld", self.pageSize];
    
    [LBNetworkingInstance requestGET:commentGroupGoodsList parameters:NSDictionaryOfVariableBindings(userId, pageIndex, pageSize) showHUD:YES success:^(id  _Nonnull responseObject, NSInteger code, NSString * _Nonnull message) {
        
        if (code == YCNetworkingStatusCodeSuccess || code == YCNetworkingStatusCodeNoneData)
        {
            NSArray *array = [NSArray yy_modelArrayWithClass:WordOfMouthModel.class json:responseObject[@"Data"]];
            statusBlock(code, array);
            [self.tableView reloadData];
        }
        else
        {
            statusBlock(code, nil);
        }
        
        
    } failure:^(NSError * _Nonnull err) {
        statusBlock(YCNetworkingStatusCodeHTTPError, nil);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    WordOfMouthModel *model = self.dataSource[indexPath.item];
    WordOfMouthCell *cell = [tableView dequeueReusableCellWithIdentifier:WordOfMouthCellIde forIndexPath:indexPath];
    [cell.coverImage sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:PLACE_HOLDER];
    cell.goodsName.text = model.name;
    cell.commentCount.text = [NSString stringWithFormat:@"%@评论", model.dpcount];
    cell.starView.starValue = [model.dpavg floatValue];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    WordOfMouthModel *model = self.dataSource[indexPath.item];
    UserCommentListController *vc = [UserCommentListController new];
    vc.goods_id = model.ID;
    vc.store_id = model.storeId;
    [self.navigationController pushViewController:vc animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
